package com.stenio.fancat.service.behavior;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.stenio.fancat.common.errorCode.ErrorCode;
import com.stenio.fancat.domain.baby.BabyAccount;
import com.stenio.fancat.domain.baby.BabyIconImage;
import com.stenio.fancat.domain.baby.BabyPartner;
import com.stenio.fancat.domain.baby.BabyRelationship;
import com.stenio.fancat.domain.behavior.*;
import com.stenio.fancat.domain.bo.BehaviorLogBO;
import com.stenio.fancat.domain.bo.CommentsBO;
import com.stenio.fancat.domain.bo.UserPraiseBO;
import com.stenio.fancat.domain.user.UserPraise;
import com.stenio.fancat.domain.user.UserRelationship;
import com.stenio.fancat.domain.user.UserRelationshipFomId;
import com.stenio.fancat.domain.user.UserRelationshipName;
import com.stenio.fancat.exception.FanCatException;
import com.stenio.fancat.form.behavior.BehaviorCommentsForm;
import com.stenio.fancat.form.behavior.BehaviorForm;
import com.stenio.fancat.form.behavior.BehaviorLogForm;
import com.stenio.fancat.form.user.UserRelationshipForm;
import com.stenio.fancat.mapper.Behavior.*;
import com.stenio.fancat.mapper.Behavior.BehaviorMapper;
import com.stenio.fancat.mapper.baby.BabyAccountMapper;
import com.stenio.fancat.mapper.baby.BabyIconImageMapper;
import com.stenio.fancat.mapper.baby.BabyPartnerMapper;
import com.stenio.fancat.mapper.baby.BabyRelationshipMapper;
import com.stenio.fancat.mapper.user.*;
import com.stenio.fancat.service.OssService;
import com.stenio.fancat.service.wx.WxPushServiceQcl;
import com.stenio.fancat.util.DateUtil;
import com.stenio.fancat.util.EmptyUtil;
import com.stenio.fancat.util.RedisUtil;
import com.stenio.fancat.util.StringUtil;
import com.stenio.fancat.vo.BabyPartnerListVO;
import com.stenio.fancat.vo.BehaviorCommentsVO;
import com.stenio.fancat.vo.BehaviorLogVO;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.*;

@Service
public class BehaviorLogService extends ServiceImpl<BehaviorLogMapper, BehaviorLog> {

    @Autowired
    private BehaviorLogMapper behaviorLogMapper;

    @Autowired
    private BabyRelationshipMapper babyRelationshipMapper;

    @Autowired
    private UserRelationshipMapper userRelationshipMapper;

    @Autowired
    private BehaviorImageMapper behaviorImageMapper;

    @Autowired
    private UserRelationshipNameMapper userRelationshipNameMapper;

    @Autowired
    private BehaviorOperationImageMapper behaviorOperationImageMapper;

    @Autowired
    private BehaviorCommentsMapper behaviorCommentsMapper;

    @Autowired
    private UserPraiseMapper userPraiseMapper;

    @Autowired
    private BabyAccountMapper babyMapper;

    @Autowired
    private OssService ossService;

    @Autowired
    private BehaviorMapper behaviorMapper;

    @Autowired
    private BabyAccountMapper babyAccountMapper;

    @Autowired
    private UserRelationshipLogMapper userRelationshipLogMapper;

    @Autowired
    private UserRelationshipCandyMapper userRelationshipCandyMapper;

    @Autowired
    private BabyPartnerMapper babyPartnerMapper;

    @Autowired
    private BabyIconImageMapper babyIconImageMapper;

    @Autowired
    private UserRelationshipFomIdMapper userRelationshipFomIdMapper;

    @Autowired
    private WxPushServiceQcl wxPushServiceQcl;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Value("${oss.piggybank.endpoint}")
    private String piggybankEndPoint;
    //亲人圈的key
    @Value("${ownBehaviorList}")
    private String key;
    //不分类亲人圈的key
    @Value("${ownBehaviorListNoClassify}")
    private String keyNoClassify;
    //伙伴圈的key
    @Value("${partnerList}")
    private String partnerListKey;
    //伙伴动态详情的key
    @Value("${partnerBehaviorList}")
    private String partnerBehaviorList;
    //首页的key
    @Value("${firstPage}")
    private String firstPageKey;
    //baby亲人列表的key
    @Value("${listKey}")
    private String listKey;
    //每个baby信息的key
    @Value("${key}")
    private String everyBabyInfo;
    //微信通知的评论模板
    @Value("${templateComment}")
    private String templateComment;
    //微信通知的发布模板
    @Value("${templatePic}")
    private String templatePic;


    /**
     * 查看亲人圈  -------->  展示所有列表   活动运营图
     * @param userRelationshipForm
     * @return
     */
    public Map<String,Object> selectOwnBehaviorList(UserRelationshipForm userRelationshipForm, BehaviorForm behaviorForm) throws FanCatException {
        if(EmptyUtil.isEmpty(userRelationshipForm)){
            throw new FanCatException("参数异常", ErrorCode.parameters);
        }

        Integer num1 = userRelationshipForm.getCurrentPageNo();
        Integer num2 = userRelationshipForm.getPageSize();
//        //判断Redis中是否含有key
//        if(EmptyUtil.isNotEmpty(redisUtil.get(userRelationshipForm.getBabyId()+key+behaviorForm.getBehaviorId()+num1+"-"+num2))){
//            return (Map<String, Object>) redisUtil.get(userRelationshipForm.getBabyId()+key+behaviorForm.getBehaviorId()+num1+"-"+num2);
//        }

        // map容器
        Map<String,Object> map = new HashMap<>();

        //查询该任务模板的名字
        Behavior b = behaviorMapper.selectBehaviors(behaviorForm.getBehaviorId());
        map.put("behaviorName",b.getName());
        map.put("describe",b.getDescribe());
        //查询该任务的运营图
        BehaviorImage behaviorImage = new BehaviorImage();
        behaviorImage.setBehaviorId(behaviorForm.getBehaviorId());
        //封装任务的运营图
        map.put("behaviorImage",behaviorImageMapper.selectOne(behaviorImage));

        //封装改任务模板下的所有任务，倒序展示
        List<BehaviorLogBO> logBOSet = new ArrayList<>();

        //查询和该baby关联的user
        List<BabyRelationship> babyRelationship = babyRelationshipMapper.selectBabyByUid(userRelationshipForm.getUserId());
        if(EmptyUtil.isEmpty(babyRelationship)){
            throw new FanCatException("没有添加baby信息", ErrorCode.addBaby);
        }

        //查询baby昵称
        String babyName = babyMapper.selectByBid(userRelationshipForm.getBabyId()).getNickName();
        map.put("babyName",babyName);
        List<BabyRelationship> list = babyRelationshipMapper.selectUserListByBabyId(userRelationshipForm.getBabyId());
        //查询出和该baby关联的user的id
        List<String> stringList = new ArrayList<>();
        for(BabyRelationship brs:list){
            stringList.add(brs.getUserId());
        }

        //sql语句直接进行in查询
        //page  分页
        BehaviorLog behaviorLog = new BehaviorLog();
        //limit
        Page<BehaviorLog> page = new Page<>(userRelationshipForm.getCurrentPageNo(),userRelationshipForm.getPageSize());
        if(EmptyUtil.isEmpty(page))
            throw new FanCatException("分页参数异常",ErrorCode.pageError);
        Set<String> set = new HashSet<>();
        for(int a = 0;a<page.getSize();a++){
            set.add("create_time");
        }
        Wrapper<BehaviorLog> eWrapper = new EntityWrapper<>(behaviorLog).orderDesc(set).eq("behavior_id",behaviorForm.getBehaviorId()).eq("baby_id",userRelationshipForm.getBabyId()).in("user_id",stringList);
        Page<BehaviorLog> roleDOList = selectPage(page, eWrapper);
        for(int i =0;i<roleDOList.getRecords().size();i++){
            roleDOList.getRecords().get(i).setId(null);
        }

        for(BehaviorLog bll : roleDOList.getRecords()){

            BabyRelationship babyRelationship1 = babyRelationshipMapper.selectBabyByUidAndBid(bll.getUserId(),bll.getBabyId());
            UserRelationship userRelationship = userRelationshipMapper.selectUserInfoById(bll.getUserId());
            UserRelationshipName userRelationshipName = new UserRelationshipName();
            userRelationshipName.setRelationship(babyRelationship1.getRelationship());
            UserRelationshipName urn = userRelationshipNameMapper.selectOne(userRelationshipName);
            BehaviorLogBO behaviorLogBO = new BehaviorLogBO();
            UserRelationship urts = new UserRelationship();
            urts.setIcon(userRelationship.getIcon());//封装用户头像
            urts.setName(userRelationship.getName());//封装用户的昵称
            UserRelationshipName userRelation = new UserRelationshipName(); //封装用户name
            userRelation.setRelationshipName(urn.getRelationshipName());
            behaviorLogBO.setUserRelationship(urts);
            behaviorLogBO.setUserRelationshipName(userRelation.getRelationshipName());
            //封装每人的任务
            behaviorLogBO.setBehaviorLog(bll);
            //封装任务的图片list
            behaviorLogBO.setBehaviorOperationImage(this.selectImages(bll.getOperationId()));
            //封装任务的评论列表
            behaviorLogBO.setBehaviorComments(this.selectComments(bll));
            //封装点赞列表
            behaviorLogBO.setUserPraiseBOList(this.selectPraise(bll));
            //查询当前用户是否对列表里面的任务点过赞
            UserPraise userPraise = userPraiseMapper.selectPraiseById(bll.getOperationId(),userRelationshipForm.getUserId());

            if(null != userPraise){
                behaviorLogBO.setUserPraiseStatus(1);  //点过赞
            }else{
                behaviorLogBO.setUserPraiseStatus(0);  //没点过赞
            }

            logBOSet.add(behaviorLogBO);
        }

        //封装BO对象
        map.put("behaviorLogBO",logBOSet);

//        //存放Redis中
//        redisUtil.set(userRelationshipForm.getBabyId()+key+behaviorForm.getBehaviorId()+num1+"-"+num2,map);

        return map;
    }

    /**
     * 新增的   这个是不包含分类的
     * 查看亲人圈  -------->  展示所有列表   活动运营图
     * @param userRelationshipForm
     * @return map -> 结果集  ->  BehaviorLogBO
     */
        public Map<String,Object> selectOwnBehaviorListNoClassify(UserRelationshipForm userRelationshipForm) throws FanCatException {
        if(EmptyUtil.isEmpty(userRelationshipForm)){
            throw new FanCatException("参数异常",ErrorCode.parameters);
        }
        Integer num1 = userRelationshipForm.getCurrentPageNo();
        Integer num2 = userRelationshipForm.getPageSize();
        //判断Redis中是否含有key
//        if(null != redisUtil.get(userRelationshipForm.getBabyId()+keyNoClassify+num1+"-"+num2)){
//            return (Map<String, Object>) redisUtil.get(userRelationshipForm.getBabyId()+keyNoClassify+num1+"-"+num2);
//        }

        // map容器
        Map<String,Object> map = new HashMap<>();

        //封装该任务模板下的所有任务，倒序展示

        List<BehaviorLogBO> logBOSet = new ArrayList<>();

        //查询和该baby关联的user
        List<BabyRelationship> babyRelationship = babyRelationshipMapper.selectBabyByUid(userRelationshipForm.getUserId());
        if(EmptyUtil.isEmpty(babyRelationship)){
            throw new FanCatException("没有添加baby信息",ErrorCode.addBaby);
        }
        List<BabyRelationship> list = babyRelationshipMapper.selectUserListByBabyId(userRelationshipForm.getBabyId());
        //查询出和该baby关联的user的id
        List<String> stringList = new ArrayList<>();
        for(BabyRelationship brs:list){
            stringList.add(brs.getUserId());
        }

        //sql语句直接进行in查询
        //page  分页
        BehaviorLog behaviorLog = new BehaviorLog();
        //limit
        Page<BehaviorLog> page = new Page<>(userRelationshipForm.getCurrentPageNo(),userRelationshipForm.getPageSize());
        if(EmptyUtil.isEmpty(page))
            throw new FanCatException("分页参数异常",ErrorCode.pageError);
        Set<String> set = new HashSet<>();
        for(int a = 0;a<page.getSize();a++){
            set.add("create_time");
        }
        Wrapper<BehaviorLog> eWrapper = new EntityWrapper<>(behaviorLog).orderDesc(set).eq("baby_id",userRelationshipForm.getBabyId()).in("user_id",stringList);
        Page<BehaviorLog> roleDOList = selectPage(page, eWrapper);
        for(int i =0;i<roleDOList.getRecords().size();i++){
            roleDOList.getRecords().get(i).setId(null);
        }

        for(int i = 0;i<roleDOList.getRecords().size();i++){
            BehaviorLogBO behaviorLogBO = new BehaviorLogBO();
            BehaviorLog log = new BehaviorLog();
            //发布任务的描述
            log.setBehaviorDescribe(roleDOList.getRecords().get(i).getBehaviorDescribe());
            //发布任务的时间
            log.setCreateTime(roleDOList.getRecords().get(i).getCreateTime());
            //可见范围
            log.setVisible(roleDOList.getRecords().get(i).getVisible());
            //任务的标题
            String bName = behaviorMapper.selectAllBehaviorList(roleDOList.getRecords().get(i).getBehaviorId()).getName();
            behaviorLogBO.setBehaviorName(bName);
            //每条任务获得的candy
            log.setCandy(roleDOList.getRecords().get(i).getCandy());
            //每条任务的behaviorId
            log.setBehaviorId(roleDOList.getRecords().get(i).getBehaviorId());
            //每条任务的operationId
            log.setOperationId(roleDOList.getRecords().get(i).getOperationId());
            //userId
            log.setUserId(roleDOList.getRecords().get(i).getUserId());
            behaviorLogBO.setBehaviorLog(log);
            //info
            String babyName = babyAccountMapper.selectByBid(userRelationshipForm.getBabyId()).getNickName();
            String babyUrl = babyIconImageMapper.selectByBid(userRelationshipForm.getBabyId()).getImage();
            //baby的昵称
            behaviorLogBO.setBabyAccountName(babyName);
            //baby头像
            behaviorLogBO.setImageUrl(babyUrl);
            //user关系名称
            Integer relationship = babyRelationshipMapper.selectBabyByUidAndBid(roleDOList.getRecords().get(i).getUserId(),roleDOList.getRecords().get(i).getBabyId()).getRelationship();
            String relationshipName = userRelationshipNameMapper.selectName(relationship).getRelationshipName();
            behaviorLogBO.setUserRelationshipName(relationshipName);
            UserRelationship urts = new UserRelationship();
            //user昵称
            UserRelationship user = userRelationshipMapper.selectUserInfoById(roleDOList.getRecords().get(i).getUserId());
            urts.setName(user.getName());
            //user头像
            urts.setIcon(user.getIcon());
            behaviorLogBO.setUserRelationship(urts);

            //封装任务的图片
            behaviorLogBO.setBehaviorOperationImage(this.selectImages(roleDOList.getRecords().get(i).getOperationId()));
            //封装任务的评论列表
            behaviorLogBO.setBehaviorComments(this.selectComments(roleDOList.getRecords().get(i)));

            //封装点赞列表
            behaviorLogBO.setUserPraiseBOList(this.selectPraise(roleDOList.getRecords().get(i)));

            //查询当前用户是否对列表里面的任务点过赞
            UserPraise userPraise = userPraiseMapper.selectPraiseById(roleDOList.getRecords().get(i).getOperationId(),userRelationshipForm.getUserId());

            if(EmptyUtil.isNotEmpty(userPraise)){
                behaviorLogBO.setUserPraiseStatus(1);  //点过赞
            }else{
                behaviorLogBO.setUserPraiseStatus(0);  //没点过赞
            }

            //封装每一个VO对象
            logBOSet.add(behaviorLogBO);
        }

        //封装BO对象
        map.put("behaviorLogBO",logBOSet);

//        //存放Redis中
//        redisUtil.set(userRelationshipForm.getBabyId()+keyNoClassify+num1+"-"+num2,map);

        return map;
    }

     /**
     * 删除个人任务  （包括任务整体，图片，评论，赞）
     * @param userRelationshipForm
     * @param behaviorLogForm
     * @return
     */
    @Transactional
    public Integer deleteBehavior(UserRelationshipForm userRelationshipForm, BehaviorLogForm behaviorLogForm) throws FanCatException {
        Integer number = 0;
        if(EmptyUtil.isEmpty(userRelationshipForm) && EmptyUtil.isEmpty(behaviorLogForm)){
            throw new FanCatException("参数异常",ErrorCode.parameters);
        }

        //判断当前user发布的当前任务是否获得了candy，如果获得了，减掉相应的candy
        Integer candy = behaviorLogMapper.selectBehaviorLogByTwoId(userRelationshipForm.getUserId(),behaviorLogForm.getOperationId()).getCandy();
        if(candy != 0){
            //先修改log表
            Integer babyCandy = babyMapper.selectByBid(userRelationshipForm.getBabyId()).getSurplusCandy();
            userRelationshipLogMapper.subtractCandy(candy,userRelationshipForm.getUserId(),babyCandy,userRelationshipForm.getBabyId());
            babyMapper.subtractCandy(candy,userRelationshipForm.getBabyId());
            userRelationshipCandyMapper.subtractCandy(candy,userRelationshipForm.getUserId(),userRelationshipForm.getBabyId());

            //操作Redis
            Set<String> keySec = redisTemplate.keys(everyBabyInfo + userRelationshipForm.getBabyId() + "*");
            redisTemplate.delete(keySec);

        }
        //删除任务整体
        Integer a=  behaviorLogMapper.deleteBehaviorByOid(userRelationshipForm.getUserId(),behaviorLogForm.getOperationId());

        //删除任务图片  (OSS服务器上的图片是否能删除再看)
        Integer b=  behaviorOperationImageMapper.deleteBehaviorOperationImages(behaviorLogForm.getOperationId());

        //删除评论列表
        Integer c= -1;
        if(behaviorCommentsMapper.selectComments(behaviorLogForm.getOperationId()) != null){
            c=  behaviorCommentsMapper.deleteComments(behaviorLogForm.getOperationId());
        }

        //删除点赞列表
        Integer d= -1;
        if(userPraiseMapper.selectPraise(behaviorLogForm.getOperationId()) != null){
            d=  userPraiseMapper.deletePraise(behaviorLogForm.getOperationId());
        }

        //操作Redis
        Set<String> keys = redisTemplate.keys(userRelationshipForm.getBabyId() + "fancat" + "*");
        redisTemplate.delete(keys);

        if((a>0 && b>0 && c>0 && d>0) || (a>0 && b>0 && c==-1 && d==-1) ){
            number = 1;
            return number;
        }

        return number;
    }


    /**
     * 新增评论
     * @param behaviorCommentsForm
     * @return
     */
    @Transactional
    public BehaviorCommentsVO insertComments(BehaviorCommentsForm behaviorCommentsForm) throws FanCatException {
        if(EmptyUtil.isEmpty(behaviorCommentsForm))
            throw new FanCatException("参数异常",ErrorCode.parameters);

        Integer i = behaviorCommentsMapper.insertComments(behaviorCommentsForm);
        if(i <= 0)
            throw new FanCatException("插入失败",ErrorCode.addCommentsError);

        BehaviorCommentsVO behaviorCommentsVO = new BehaviorCommentsVO();

        //更新评论
        BehaviorComments behaviorComments = new BehaviorComments();

        try {
            BeanUtils.copyProperties(behaviorComments,behaviorCommentsForm);
        } catch (IllegalAccessException e){
            e.printStackTrace();
        } catch (InvocationTargetException e){
            e.printStackTrace();
        }
        //评论创建时间和内容
        BehaviorComments comments = behaviorCommentsMapper.selectRecent(behaviorComments.getOperationId());
        comments.setUpdateTime(null);
        behaviorCommentsVO.setBehaviorComments(comments);

        //查询user  info   (babyName)
        UserRelationship userRelationship = userRelationshipMapper.selectUserInfoById(behaviorCommentsForm.getUserId());
        BabyRelationship babyRelationship = babyRelationshipMapper.selectBabyByUidAndBid(behaviorCommentsForm.getUserId(),behaviorCommentsForm.getBabyId());
        //判断当前user是否绑定了baby
        if(EmptyUtil.isNotEmpty(babyRelationship)){
            BabyAccount babyinfo = babyMapper.selectByBid(behaviorCommentsForm.getBabyId());
            behaviorCommentsVO.setBabyName(babyinfo.getNickName());
            behaviorCommentsVO.setUserName(userRelationship.getName());
            UserRelationshipName user = new UserRelationshipName();
            user.setRelationship(babyRelationship.getRelationship());
            UserRelationshipName u = userRelationshipNameMapper.selectOne(user);
            behaviorCommentsVO.setRelationshipName(u.getRelationshipName());
        }else{
            behaviorCommentsVO.setUserName(userRelationship.getName());
        }

        //查询outUser  info
        if(EmptyUtil.isNotEmpty(behaviorComments.getOutUserId())){
            UserRelationship userRelationship1 = userRelationshipMapper.selectUserInfoById(behaviorCommentsForm.getOutUserId());
            BabyRelationship babyRelationshipSec = babyRelationshipMapper.selectBabyByUidAndBid(behaviorCommentsForm.getUserId(),behaviorCommentsForm.getBabyId());
            //判断当前user是否绑定了baby
            if(EmptyUtil.isNotEmpty(babyRelationshipSec)){
                BabyAccount babyinfoSec = babyMapper.selectByBid(behaviorCommentsForm.getOutBabyId());
                behaviorCommentsVO.setBabyNameSec(babyinfoSec.getNickName());
                behaviorCommentsVO.setUserNameSec(userRelationship1.getName());
                UserRelationshipName user1 = new UserRelationshipName();
                user1.setRelationship(babyRelationshipSec.getRelationship());
                UserRelationshipName u1 = userRelationshipNameMapper.selectOne(user1);
                behaviorCommentsVO.setRelationshipNameSec(u1.getRelationshipName());
            }else {
                behaviorCommentsVO.setUserNameSec(userRelationship1.getName());
            }

        }

        //发布成功后推送微信模板消息
        String[] arr = new String[5];
        arr[0] = userRelationship.getName()+"评论了您的宝宝动态";
        arr[1] = "点击查看详情";
        //arr[2] = DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss");
        //根据operationId查询该动态的userId  自己评论自己发的动态不发通知
        BehaviorLog behaviorLog = new BehaviorLog();
        behaviorLog.setOperationId(behaviorComments.getOperationId());
        BehaviorLog behaviorLogNew = behaviorLogMapper.selectOne(behaviorLog);
        String userId = behaviorLogNew.getUserId();
        if(!userId.equals(behaviorComments.getUserId())){
            String openId = userRelationshipMapper.selectUserInfoById(userId).getOpenId();
            UserRelationshipFomId userRelationshipFomId = userRelationshipFomIdMapper.selectFormIdByUid(userId);
            if(null != userRelationshipFomId){
                String formId = userRelationshipFomIdMapper.selectFormIdByUid(userId).getFormId();
                //推送通知
                wxPushServiceQcl.pushOneUser(openId,formId,templateComment,arr,behaviorCommentsForm.getPage());
                //修改这个formid的状态
                userRelationshipFomIdMapper.modifyFormIdByUid(userId,formId);
            }
        }

//        //操作Redis
//        this.operationRedis(behaviorCommentsForm.getBabyId(),behaviorCommentsForm.getOperationId());

        return behaviorCommentsVO;
    }

    /**
     * 删除个人的评论
     * @param behaviorCommentsForm
     * @return
     */
    @Transactional
    public Integer deleteComments(BehaviorCommentsForm behaviorCommentsForm) throws FanCatException {
        if(EmptyUtil.isEmpty(behaviorCommentsForm)){
            throw new FanCatException("参数异常",ErrorCode.parameters);
        }
        Integer number = 0;

        //String babyId = this.selectBabyId(behaviorCommentsForm.getUserId());

        number = behaviorCommentsMapper.deleteComment(behaviorCommentsForm.getId(),behaviorCommentsForm.getUserId());

        //操作Redis
        Set<String> keys = redisTemplate.keys(behaviorCommentsForm.getBabyId() + "fancat" + "*");
        redisTemplate.delete(keys);

        return number;
    }


    /**
     * 新建个人任务(上传图片)
     * @param multipartFile
     * @return
     */
    @Transactional
    public String insertOwnBehaviorImages(MultipartFile multipartFile,BehaviorLogForm behaviorLogForm) throws FanCatException {
        if(EmptyUtil.isEmpty(multipartFile) || EmptyUtil.isEmpty(behaviorLogForm)){
            throw new FanCatException("参数异常",ErrorCode.parameters);
        }
        //上传图片

        //生成OSS图片的url存入MySQL
        String fileName = ossService.getImageName(multipartFile.getOriginalFilename(),behaviorLogForm.getBabyId(),behaviorLogForm.getBehaviorId());

        try {
            //上传至OSS服务器
            ossService.putObject(fileName,multipartFile.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileName;
    }

    /**
     * 新建个人任务   1.操作用户表的金额   2.操作babyAccount表的金额
     * @param behaviorLogForm
     * @return
     */
    @Transactional
    public BehaviorLogVO insertOwnBehavior(BehaviorLogForm behaviorLogForm) throws FanCatException {
        if(EmptyUtil.isEmpty(behaviorLogForm)){
            throw new FanCatException("参数异常",ErrorCode.parameters);
        }

        int number = 0;

        //查询该baby关联的其他的baby
        List<BabyPartner> partners =  babyPartnerMapper.selectBabyPartnerByBid(behaviorLogForm.getBabyId());

        BehaviorLogVO behaviorLogVO = new BehaviorLogVO();

        Date date = new Date();  //生成当前日期
        //上传事件（上传至数据库）
        BehaviorLog behaviorLog = new BehaviorLog();
        behaviorLog.setCreateTime(date);
        behaviorLog.setUpdateTime(date);
        behaviorLog.setBehaviorDescribe(behaviorLogForm.getBehaviorDescribe());
        behaviorLog.setBehaviorId(behaviorLogForm.getBehaviorId());
        behaviorLog.setVisible(behaviorLogForm.getVisible());
        behaviorLog.setOperationId(StringUtil.getUUID());
        behaviorLog.setUserId(behaviorLogForm.getUserId());
        behaviorLog.setBabyId(behaviorLogForm.getBabyId());
        //查询当前用户和baby的关系，如果是爸爸或者妈妈，加candy  如果不是就不加candy
        Integer relationship = babyRelationshipMapper.selectBabyByUidAndBid(behaviorLogForm.getUserId(),behaviorLogForm.getBabyId()).getRelationship();
        if(relationship == 1 || relationship == 2){
            //查询当前user是否为第一次发表本次任务，从而决定是否给金币
            BehaviorLog behaviorLog1 = behaviorLogMapper.selectIfIssue(behaviorLogForm.getBehaviorId(),behaviorLogForm.getUserId(),behaviorLogForm.getBabyId());

            //代表在这个模板下没有发过任何的任务，第一次同样给钱
            if(null == behaviorLog1){
                behaviorLog.setCandy(behaviorLogForm.getCandy());
                behaviorLogVO.setIsGetCandy(0);  // 0,给钱  1，不给钱
            }else {
                String createTime1 = DateUtil.format(behaviorLog1.getCreateTime(),"yyyyMMddHHmmss");
                String time1 = createTime1.substring(0,8);
                String createTime2 = DateUtil.format(date,"yyyyMMddHHmmss");
                String time2 = createTime2.substring(0,8);
                if (!time1.equals(time2)) {
                    behaviorLog.setCandy(behaviorLogForm.getCandy());
                    behaviorLogVO.setIsGetCandy(0);  // 0,给钱  1，不给钱
                }else{
                    behaviorLog.setCandy(0);
                    behaviorLogVO.setIsGetCandy(1);  // 0,给钱  1，不给钱
                }
            }
        }else {
            behaviorLog.setCandy(0);
        }
        behaviorLogMapper.insert(behaviorLog);
        BehaviorLog log = behaviorLogMapper.selectById(behaviorLog);

        //上传图片（上传至数据库）

        behaviorLogForm.getListImages().stream().forEach(image -> {
            BehaviorOperationImage behaviorOperationImage = new BehaviorOperationImage();
            behaviorOperationImage.setImage(piggybankEndPoint+image);
            behaviorOperationImage.setOperationId(log.getOperationId());  //保证和本次任务的UUID相等
            behaviorOperationImage.setCreateTime(date);
            behaviorOperationImage.setUpdateTime(date);
            behaviorOperationImage.setStatus(1);
            behaviorOperationImage.setType(behaviorLogForm.getType());  //图片类型   1，图片  2，视频
            behaviorOperationImageMapper.insert(behaviorOperationImage);
        });


        //发布成功后推送微信模板消息
        List<BabyRelationship> userRelationshipList = babyRelationshipMapper.selectUserListByBabyId(behaviorLogForm.getBabyId());
        //查询宝宝的昵称和关系名称
        String nickName = babyMapper.selectByBid(behaviorLogForm.getBabyId()).getNickName();
        BabyRelationship babyRelationship = babyRelationshipMapper.selectBabyByUidAndBid(behaviorLogForm.getUserId(),behaviorLogForm.getBabyId());
        String relationName = userRelationshipNameMapper.selectName(babyRelationship.getRelationship()).getRelationshipName();
        String[] arr = new String[5];
        arr[0] = nickName+"的"+relationName+"发布了新照片，快来看！";
        arr[1] = userRelationshipMapper.selectUserInfoById(behaviorLogForm.getUserId()).getName();
        arr[2] = DateUtil.format(date,"yyyy-MM-dd HH:mm:ss");
        for(int i=0;i<userRelationshipList.size();i++){
            //跳过当前发表任务的user
            if(behaviorLogForm.getUserId().equals(userRelationshipList.get(i).getUserId()))
                continue;
            String userId = userRelationshipList.get(i).getUserId();
            UserRelationshipFomId userRelationshipFomId = userRelationshipFomIdMapper.selectFormIdByUid(userId);
            if(null != userRelationshipFomId){
                String formId = userRelationshipFomIdMapper.selectFormIdByUid(userId).getFormId();
                //推送通知
                wxPushServiceQcl.pushOneUser(userRelationshipMapper.selectUserInfoById(userRelationshipList.get(i).getUserId()).getOpenId(),formId,templatePic,arr,behaviorLogForm.getPage());
                //修改这个formid的状态
                userRelationshipFomIdMapper.modifyFormIdByUid(userId,formId);
            }
        }

        //1.操作userRelationshipCandy表的金额   2.操作babyAccount表的金额   3.操作fc_user_relationship_log表的金额
        if(log.getCandy() == 0){
                number = 1;
                behaviorLogVO.setState(number);
                behaviorLogVO.setCandy(log.getCandy());
                //操作Redis
                Set<String> keys = redisTemplate.keys(behaviorLogForm.getBabyId() + "fancat" + "*");
                redisTemplate.delete(keys);
                redisUtil.del(listKey+behaviorLogForm.getBabyId(),everyBabyInfo+behaviorLogForm.getBabyId()+behaviorLogForm.getUserId());

            return behaviorLogVO;

        }
        //BabyRelationship babyRelationship = babyRelationshipMapper.selectBabyByUid(behaviorLogForm.getUserId());

        BabyAccount babyAccount = babyAccountMapper.selectByBid(behaviorLogForm.getBabyId());
        userRelationshipLogMapper.modifyCandy(log.getCandy(),behaviorLogForm.getUserId(),babyAccount.getSurplusCandy(),behaviorLogForm.getBabyId());

        babyMapper.modifyCandy(log.getCandy(),behaviorLogForm.getBabyId());

        userRelationshipCandyMapper.modifyCandy(log.getCandy(),behaviorLogForm.getUserId(),behaviorLogForm.getBabyId());


        //操作Redis
        Set<String> keys = redisTemplate.keys(behaviorLogForm.getBabyId() + "fancat" + "*");
        redisTemplate.delete(keys);
        //删除个人信息
        Set<String> keySec = redisTemplate.keys(everyBabyInfo + behaviorLogForm.getBabyId() + "*");
        redisTemplate.delete(keySec);
        //删除其他人的伙伴圈
        partners.stream().forEach(partner -> {
            Set<String> keystwo = redisTemplate.keys(partner.getBabyId() + "fancat" + "*");
            redisTemplate.delete(keystwo);
        });
        redisUtil.del(listKey+behaviorLogForm.getBabyId());

        number = 1;
        behaviorLogVO.setState(number);
        behaviorLogVO.setCandy(log.getCandy());

        return behaviorLogVO;
    }


    /**
     * 查询伙伴圈
     * @param userRelationshipForm
     * @return
     * @throws FanCatException
     */
    public Map<String,Object> selectBehaviorList(UserRelationshipForm userRelationshipForm) throws FanCatException {
        if(EmptyUtil.isEmpty(userRelationshipForm)){
            throw new FanCatException("参数异常",ErrorCode.parameters);
        }
//        Integer num1 = userRelationshipForm.getCurrentPageNo();
//        Integer num2 = userRelationshipForm.getPageSize();

//        //判断Redis中是否含有key
//        if(null != redisUtil.get(userRelationshipForm.getBabyId()+partnerListKey+num1+"-"+num2)){
//            return (Map<String, Object>) redisUtil.get(userRelationshipForm.getBabyId()+partnerListKey+num1+"-"+num2);
//        }

        //封装结果容器
        Map<String,Object> map = new HashMap<>();
        List<BabyPartnerListVO> voList = new ArrayList<>();

        //查询user关联baby的id
        List<BabyRelationship> babyRelationship = babyRelationshipMapper.selectBabyByUid(userRelationshipForm.getUserId());
        if(EmptyUtil.isEmpty(babyRelationship)) {
            throw new FanCatException("请添加baby信息",ErrorCode.addBaby);
        }

        //查询baby关联的伙伴的babyId
        List<BabyPartner> partnerList = babyPartnerMapper.selectBabyPartnerByBid(userRelationshipForm.getBabyId());
        if((EmptyUtil.isEmpty(partnerList))) {
            throw new FanCatException("伙伴圈没有任何内容",ErrorCode.norelationOfPartner);
        }

        //查询baby关联的伙伴的babyId关联的userId
        //封装关联baby的所有的userId
        List<String> userIdList = new ArrayList<>();
        //封装当前用户的userId
        List<BabyRelationship> partnerUserList = null;
        for(int i=0;i<partnerList.size();i++){
            if(!partnerList.get(i).getAssociatedBabyId().equals(userRelationshipForm.getBabyId())){
                partnerUserList = babyRelationshipMapper.selectUserListByBabyId(partnerList.get(i).getAssociatedBabyId());
            }else{
                partnerUserList = babyRelationshipMapper.selectUserListByBabyId(partnerList.get(i).getBabyId());
            }
            for(int j=0;j<partnerUserList.size();j++){
                userIdList.add(partnerUserList.get(j).getUserId());
            }
        }

        //查询baby关联伙伴的总数量
        Integer number = babyPartnerMapper.selectCountPartners(userRelationshipForm.getBabyId());

        map.put("friendsNumber",number);

        //sql语句直接进行in查询    page  分页
        //查询baby关联baby下所有的user的log
        BehaviorLog behaviorLog = new BehaviorLog();
        //limit
        if(null == userRelationshipForm.getCurrentPageNo() || null == userRelationshipForm.getPageSize())
            throw new FanCatException("分页参数不能为空",ErrorCode.pageError);
        Page<BehaviorLog> page = new Page<>(userRelationshipForm.getCurrentPageNo(),userRelationshipForm.getPageSize());
        Set<String> set = new HashSet<>();
        for(int a = 0;a<page.getSize();a++){
            set.add("create_time");
        }
        Wrapper<BehaviorLog> eWrapper = new EntityWrapper<>(behaviorLog).orderDesc(set).eq("visible",2).in("user_id",userIdList);
        Page<BehaviorLog> roleDOList = selectPage(page, eWrapper);
        for(int i =0;i<roleDOList.getRecords().size();i++){
            roleDOList.getRecords().get(i).setId(null);
            roleDOList.getRecords().get(i).setUpdateTime(null);
        }

        //封装伙伴圈每一个任务对象
        for(int i = 0;i<roleDOList.getRecords().size();i++){
            BabyPartnerListVO babyPartnerListVO = new BabyPartnerListVO();
            //发布任务的描述
            babyPartnerListVO.setDescribe(roleDOList.getRecords().get(i).getBehaviorDescribe());
            //发布任务的时间
            babyPartnerListVO.setCreateTime(roleDOList.getRecords().get(i).getCreateTime());
            String bName = behaviorMapper.selectBehaviors(roleDOList.getRecords().get(i).getBehaviorId()).getName();
            //可见范围
            babyPartnerListVO.setVisible(roleDOList.getRecords().get(i).getVisible());
            //任务的标题
            babyPartnerListVO.setBehaviorName(bName);
            //任务的operationId
            babyPartnerListVO.setOperationId(roleDOList.getRecords().get(i).getOperationId());
            //userId
            babyPartnerListVO.setUserId(roleDOList.getRecords().get(i).getUserId());
            String babyName = babyAccountMapper.selectByBid(roleDOList.getRecords().get(i).getBabyId()).getNickName();
            String babyImageUrl = babyIconImageMapper.selectByBid(roleDOList.getRecords().get(i).getBabyId()).getImage();
            //user昵称
            UserRelationship userRelationship = userRelationshipMapper.selectUserInfoById(roleDOList.getRecords().get(i).getUserId());
            babyPartnerListVO.setUserName(userRelationship.getName());
            //user头像
            babyPartnerListVO.setUserImageUrl(userRelationship.getIcon());
            //baby的昵称
            babyPartnerListVO.setNickName(babyName);
            //baby头像
            babyPartnerListVO.setBabyImageUrl(babyImageUrl);
            //user关系名称
            Integer relationship = babyRelationshipMapper.selectBabyByUidAndBid(roleDOList.getRecords().get(i).getUserId(),roleDOList.getRecords().get(i).getBabyId()).getRelationship();
            String relationshipName = userRelationshipNameMapper.selectName(relationship).getRelationshipName();
            babyPartnerListVO.setRelationshipName(relationshipName);

            //封装任务的图片
            babyPartnerListVO.setImages(this.selectImages(roleDOList.getRecords().get(i).getOperationId()));
            //封装任务的评论列表
            babyPartnerListVO.setComments(this.selectComments(roleDOList.getRecords().get(i)));
            //封装点赞列表
            babyPartnerListVO.setPraise(this.selectPraise(roleDOList.getRecords().get(i)));

            //查询当前用户是否对列表里面的任务点过赞
            UserPraise userPraise = userPraiseMapper.selectPraiseById(roleDOList.getRecords().get(i).getOperationId(),userRelationshipForm.getUserId());

            if(null != userPraise){
                babyPartnerListVO.setUserPraiseStatus(1);  //点过赞
            }else{
                babyPartnerListVO.setUserPraiseStatus(0);  //没点过赞
            }

            //封装每一个VO对象
            voList.add(babyPartnerListVO);
        }
        map.put("BabyPartnerListVO",voList);

//        //存放Redis中
//        redisUtil.set(userRelationshipForm.getBabyId()+partnerListKey+num1+"-"+num2,map);
        return map;
    }


    /**
     * 查询伙伴动态详情
     * @param userRelationshipForm
     * @return
     */
    public List<BehaviorLogBO> selectPartnerBehaviorList(UserRelationshipForm userRelationshipForm) throws FanCatException {
        if(EmptyUtil.isEmpty(userRelationshipForm.getBabyId()) || EmptyUtil.isEmpty(userRelationshipForm.getCurrentPageNo()) || EmptyUtil.isEmpty(userRelationshipForm.getPageSize())){
            throw new FanCatException("参数异常",ErrorCode.parameters);
        }
//        Integer num1 = userRelationshipForm.getCurrentPageNo();
//        Integer num2 = userRelationshipForm.getPageSize();

//        //判断Redis中是否含有key
//        if(null != redisUtil.get(userRelationshipForm.getBabyId()+partnerBehaviorList+num1+"-"+num2)){
//            return (List<BehaviorLogBO>) redisUtil.get(userRelationshipForm.getBabyId()+partnerBehaviorList+num1+"-"+num2);
//        }

        //查询和baby关联的user
        List<BabyRelationship> babyRelationshipList = babyRelationshipMapper.selectUserListByBabyId(userRelationshipForm.getBabyId());
        List<String> userIdList =  new ArrayList<>();
        babyRelationshipList.stream().forEach(brs -> {
            userIdList.add(brs.getUserId());
        });

        //封装改任务模板下的所有任务，倒序展示
        List<BehaviorLogBO> logBOSet = new ArrayList<>();

        //sql语句直接进行in查询
        //page  分页
        BehaviorLog behaviorLog = new BehaviorLog();
        //limit
        Page<BehaviorLog> page = new Page<>(userRelationshipForm.getCurrentPageNo(),userRelationshipForm.getPageSize());
        if(null == page)
            throw new FanCatException("分页参数异常",ErrorCode.pageError);
        Set<String> set = new HashSet<>();
        for(int a = 0;a<page.getSize();a++){
            set.add("create_time");
        }
        Wrapper<BehaviorLog> eWrapper = new EntityWrapper<>(behaviorLog).orderDesc(set).eq("baby_id",userRelationshipForm.getBabyId()).in("user_id",userIdList);
        Page<BehaviorLog> roleDOList = selectPage(page, eWrapper);
        for(int i =0;i<roleDOList.getRecords().size();i++){
            roleDOList.getRecords().get(i).setId(null);
        }


        for(BehaviorLog bll :  roleDOList.getRecords()){
            UserRelationship userRelationship = userRelationshipMapper.selectUserInfoById(bll.getUserId());
            BabyRelationship babyRelationship = babyRelationshipMapper.selectBabyByUidAndBid(bll.getUserId(),bll.getBabyId());
            UserRelationshipName userRelationshipName = new UserRelationshipName();
            userRelationshipName.setRelationship(babyRelationship.getRelationship());
            UserRelationshipName urn = userRelationshipNameMapper.selectOne(userRelationshipName);
            BehaviorLogBO behaviorLogBO = new BehaviorLogBO();
            //封装baby的信息
            behaviorLogBO.setBabyAccountName(babyAccountMapper.selectByBid(userRelationshipForm.getBabyId()).getNickName());
            BabyIconImage babyIconImage = babyIconImageMapper.selectByBid(userRelationshipForm.getBabyId());
            behaviorLogBO.setImageUrl(babyIconImage.getImage());
            behaviorLogBO.setUserImageUrl(userRelationship.getIcon());
            //user
            behaviorLogBO.setUserRelationship(userRelationship);
            //封装用户关系name
            behaviorLogBO.setUserRelationshipName(urn.getRelationshipName());
            //查询该任务模板的名字
            Behavior behavior = behaviorMapper.selectBehaviors(bll.getBehaviorId());
            behaviorLogBO.setBehaviorName(behavior.getName());
            //封装operationId和describe
            behaviorLogBO.setOperationId(bll.getOperationId());
            behaviorLogBO.setDescribe(bll.getBehaviorDescribe());
            //封装任务的图片list
            behaviorLogBO.setBehaviorOperationImage(this.selectImages(bll.getOperationId()));
            //封装任务的评论列表
            behaviorLogBO.setBehaviorComments(this.selectComments(bll));

            //封装点赞列表
            behaviorLogBO.setUserPraiseBOList(this.selectPraise(bll));

            //封装创建时间
            behaviorLogBO.setCreateTime(bll.getCreateTime());

            //查询当前用户是否对列表里面的任务点过赞
            UserPraise userPraise = userPraiseMapper.selectPraiseById(bll.getOperationId(),userRelationshipForm.getUserId());

            if(null != userPraise){
                behaviorLogBO.setUserPraiseStatus(1);  //点过赞
            }else{
                behaviorLogBO.setUserPraiseStatus(0);  //没点过赞
            }

            logBOSet.add(behaviorLogBO);
        }

//        //存放Redis中
//        redisUtil.set(userRelationshipForm.getBabyId()+partnerBehaviorList+num1+"-"+num2,logBOSet);

        return logBOSet;
    }

    /**
     * 评论操作Redis的方法
     * @param babyId
     * @param operationId
     * @throws FanCatException
     */
    @Transactional
    public void operationRedis(String babyId,String operationId) {

        Set<String> keys = redisTemplate.keys(babyId + "fancat" + "*");
        redisTemplate.delete(keys);
        //查询该baby关联的其他的baby
        List<BabyPartner> partners =  babyPartnerMapper.selectBabyPartnerByBid(babyId);
        //删除其他人的伙伴圈
        BehaviorLog behaviorLog = new BehaviorLog();
        behaviorLog.setOperationId(operationId);
        BehaviorLog behaviorLogNew = behaviorLogMapper.selectOne(behaviorLog);
        if (behaviorLogNew.getVisible() == 2) {
            partners.stream().forEach(partner -> {
                Set<String> keystwo = redisTemplate.keys(partner.getBabyId() + "fancat" + "*");
                redisTemplate.delete(keystwo);
            });
        }
        //删除伙伴动态详情的Redis
        BehaviorLog log = new BehaviorLog();
        log.setOperationId(operationId);
        String babyPartnerId = behaviorLogMapper.selectOne(log).getBabyId();
        Set<String> keysSec = redisTemplate.keys(babyPartnerId+ "fancat" + "*");
        redisTemplate.delete(keysSec);

    }

    /**
     * 封装点赞列表
     * @param behaviorLog
     * @return
     */
    public List<UserPraiseBO> selectPraise(BehaviorLog behaviorLog){
        List<UserPraise> praises = userPraiseMapper.selectPraise(behaviorLog.getOperationId());
        List<UserPraiseBO> userPraiseBOList = new ArrayList<>();
        praises.stream().forEach(pra -> {
            UserPraiseBO userPraiseBO = new UserPraiseBO();
            BabyRelationship babyRelationship = babyRelationshipMapper.selectBabyByUidAndBid(pra.getUserId(),pra.getBabyId());
            UserRelationship usercom = userRelationshipMapper.selectUserInfoById(pra.getUserId());
            if(EmptyUtil.isNotEmpty(babyRelationship.getRelationship())){
                UserRelationshipName uname = new UserRelationshipName();
                uname.setRelationship(babyRelationship.getRelationship());
                UserRelationshipName urname = userRelationshipNameMapper.selectOne(uname);
                String nickName = babyMapper.selectByBid(babyRelationship.getBabyId()).getNickName();
                userPraiseBO.setUserRelationshipName(urname.getRelationshipName());
                userPraiseBO.setBabyName(nickName);
            }else{
                userPraiseBO.setUserName(usercom.getName());
            }
            userPraiseBOList.add(userPraiseBO);
        });

        return userPraiseBOList;
    }

    /**
     * 封装任务的评论列表
     * @param behaviorLog
     * @return
     */
    public List<CommentsBO> selectComments(BehaviorLog behaviorLog){
        List<BehaviorComments> comments = behaviorCommentsMapper.selectComments(behaviorLog.getOperationId());
        //封装评论的容器
        List<CommentsBO> commentsBOList = new ArrayList<>();
        comments.stream().forEach(com -> {
            CommentsBO commentsBO = new CommentsBO();
            com.setUpdateTime(null);
            com.setCreateTime(null);

            BabyRelationship babyRelationship = babyRelationshipMapper.selectBabyByUidAndBid(com.getUserId(),com.getBabyId());
            BabyRelationship babyRelationshipSec = babyRelationshipMapper.selectBabyByUidAndBid(com.getOutUserId(),com.getOutBabyId());

            //先判断user是否绑定了baby
            if(EmptyUtil.isEmpty(babyRelationship.getRelationship()) || EmptyUtil.isEmpty(babyRelationship.getBabyId())){
                UserRelationship user = userRelationshipMapper.selectUserInfoById(com.getUserId());
                commentsBO.setUserName(user.getName());
            }

            //查询1级评论人的关系名称
            if(EmptyUtil.isNotEmpty(babyRelationship.getRelationship())){
                UserRelationshipName uname = new UserRelationshipName();
                uname.setRelationship(babyRelationship.getRelationship());
                UserRelationshipName urnames = userRelationshipNameMapper.selectOne(uname);
                urnames.setId(null);
                urnames.setCreateTime(null);
                urnames.setUpdateTime(null);
                commentsBO.setUserRelationshipName(urnames);
            }

            //封装baby对象  (1级评论的baby对象)
            if(EmptyUtil.isNotEmpty(babyRelationship.getBabyId())){
                BabyAccount babyAccount = babyMapper.selectByBid(babyRelationship.getBabyId());
                babyAccount.setTotalCandy(null);
                babyAccount.setSurplusCandy(null);
                babyAccount.setCreateTime(null);
                babyAccount.setUpdateTime(null);
                babyAccount.setBabyId(null);
                babyAccount.setGender(null);
                babyAccount.setStatus(null);
                babyAccount.setVersion(null);
                babyAccount.setId(null);
                babyAccount.setBirthday(null);
                commentsBO.setBabyAccount(babyAccount);
            }
            //封装评论内容
            commentsBO.setBehaviorComments(com);

            //查询2级评论人的name
            if (EmptyUtil.isNotEmpty(babyRelationshipSec)) {
                //先判断user是否绑定了baby
                if(EmptyUtil.isEmpty(babyRelationshipSec.getRelationship()) || EmptyUtil.isEmpty(babyRelationshipSec.getBabyId())){
                    UserRelationship user = userRelationshipMapper.selectUserInfoById(com.getOutUserId());
                    commentsBO.setOutUserName(user.getName());
                }
                //查询2级评论人的关系名称
                if(EmptyUtil.isNotEmpty(babyRelationshipSec.getRelationship())){
                    UserRelationshipName unameSec = new UserRelationshipName();
                    unameSec.setRelationship(babyRelationshipSec.getRelationship());
                    UserRelationshipName urnameSecs = userRelationshipNameMapper.selectOne(unameSec);
                    urnameSecs.setId(null);
                    urnameSecs.setCreateTime(null);
                    urnameSecs.setUpdateTime(null);
                    commentsBO.setOutUserRelationshipName(urnameSecs.getRelationshipName());
                }


                //封装baby对象  (2级评论的baby对象)
                if(EmptyUtil.isNotEmpty(babyRelationshipSec.getBabyId())){
                    BabyAccount babyAccountSec = babyMapper.selectByBid(babyRelationshipSec.getBabyId());
                    babyAccountSec.setTotalCandy(null);
                    babyAccountSec.setSurplusCandy(null);
                    babyAccountSec.setCreateTime(null);
                    babyAccountSec.setUpdateTime(null);
                    babyAccountSec.setBabyId(null);
                    babyAccountSec.setGender(null);
                    babyAccountSec.setStatus(null);
                    babyAccountSec.setVersion(null);
                    babyAccountSec.setId(null);
                    babyAccountSec.setBirthday(null);
                    commentsBO.setOutBabyName(babyAccountSec.getNickName());
                    //commentsBO.setOutUserName(usercomSec.getName());
                }

            }

            commentsBOList.add(commentsBO);
        });

        return commentsBOList;
    }

    /**
     * 封装图片集合
     * @param operationId
     * @return
     */
    public List<BehaviorOperationImage> selectImages(String operationId){
        //封装任务的图片list
        List<BehaviorOperationImage> images = behaviorOperationImageMapper.selectBehaviorOperationImages(operationId);
        //把图片按照上传的时间顺序排列出来
        Long[] arr = new Long[images.size()];
        for(int i=0; i<images.size(); i++){
            String url = images.get(i).getImage();
            int index = url.lastIndexOf("/");
            int end = url.lastIndexOf(".");
            arr[i] = Long.parseLong(url.substring(index+1,end));
        }
        //进行排序
        Arrays.sort(arr);
        //创建排序之后的容器
        List<BehaviorOperationImage> imageList = new ArrayList<>();
        for(int a = 0;a<arr.length; a++){
            images.get(a).setUpdateTime(null);
            images.get(a).setId(null);
            images.get(a).setOperationId(null);
            images.get(a).setStatus(null);
            images.get(a).setCreateTime(null);
            imageList.add(images.get(a));
        }
        return imageList;
    }

}
